<template>
  <div v-if="show">
    <el-dialog :visible.sync="dialogVisible" fullscreen append-to-body>
      <el-container>
        <el-main style="height:calc(100vh  - 160px); padding: 0 45px;">
          <div class="score">
            <el-button type="primary" @click="exportExcel(cepingHzglb.hzName)">导出统计表</el-button>
          </div>
          <div class="customTableHeader">
            <div class="customTableTitle">{{ cepingHzglb.hzName }}</div>
          </div>
          <div class="cpTable">
            <el-table ref='cpphztj-table' :data="cepingHzglDxpzbList" border stripe height="calc(100vh - 287px)">
              <el-table-column label="序号" width="100" type="index">
              </el-table-column>
              <el-table-column prop="deptName" label="部门" min-width="200" header-align="center" align="center">
                <template slot-scope="scope">
                  <span v-if="cepingHzglb.hzDx=='个人'">
                    {{ scope.row.deptName }}
                  </span>
                  <span v-else style="color: #3e51a3;cursor: pointer;" @click="viewMx(scope.row)">
                    {{ scope.row.deptName }}
                  </span>
                </template>
              </el-table-column>
              <el-table-column prop="userName" label="姓名" min-width="200" header-align="center" align="center"
                               v-if="cepingHzglb.hzDx=='个人'">
                <template slot-scope="scope">
                  <span style="color: #3e51a3;cursor: pointer;" @click="viewMx(scope.row)">
                    {{ scope.row.userName }}
                  </span>
                </template>
              </el-table-column>
              <el-table-column prop="userZw" label="职务" min-width="200" header-align="center" align="center"
                               v-if="cepingHzglb.hzDx=='个人'">
              </el-table-column>
              <el-table-column v-for="(item,index) in cepingHzglHdfwbList" :key="index" :prop="'hd_'+item.hdId"
                               :label="item.hdHzmc" min-width="200" header-align="center" align="center">
              </el-table-column>
              <el-table-column prop="df" label="综合得分" min-width="200" header-align="center" align="center">
                <template slot-scope="scope">
                  <span style="color: #3e51a3;cursor: pointer;" @click="showMx(scope.row)">
                    {{ scope.row.df }}
                  </span>
                </template>
              </el-table-column>
              <el-table-column prop="jg" label="测评结果" min-width="150" header-align="center" align="center" v-if="pmShow">
              </el-table-column>
            </el-table>
          </div>
        </el-main>
      </el-container>
      <span slot="footer" class="dialog-footer flex justifyContentSpaceBetween">
        <div class="flex alignItemsCenter">
          <div class="font16">
            排名设置：<el-checkbox v-model="pmShow">显示排名</el-checkbox>
          </div>
          <div v-if="pmShow" style="margin:0 10px">
            <el-button type="primary" @click="szDialogVisible = true">排名设置</el-button>
          </div>
        </div>
        <el-button @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
    <el-dialog title="排名设置" :visible.sync="szDialogVisible" width="370px" append-to-body>
      <el-container>
        <el-main style="max-height:60vh">
          <div style="margin-bottom:20px;margin-left: 30px;">
            <el-input v-model="pmName[0]" style="width: 100px;" :disabled="true"></el-input>
            <el-input-number v-model="pm[0]" :min="0" class="ml10"></el-input-number>
            %
            <div v-show="pm[0] > 100" class="red">* 输入的范围小于100%</div>
          </div>
          <div style="margin-bottom:20px;margin-left: 30px;">
            <el-input v-model="pmName[1]" style="width: 100px;" :disabled="true"></el-input>
            <el-input-number v-model="pm[1]" :min="0" class="ml10"></el-input-number>
            %
            <div v-show="pm[1] > 100" class="red">* 输入的范围小于100%</div>
          </div>
          <div style="margin-bottom:20px;margin-left: 30px;">
            <el-input v-model="pmName[2]" style="width: 100px;" :disabled="true"></el-input>
            <el-input-number v-model="pm[2]" :min="0" class="ml10"></el-input-number>
            %
            <div v-show="pm[2] > 100" class="red">* 输入的范围小于100%</div>
          </div>
          <div style="margin-bottom:20px;margin-left: 30px;">
            <el-input v-model="pmName[3]" style="width: 100px;" :disabled="true"></el-input>
            <el-input-number v-model="pm[3]" :min="0" class="ml10"></el-input-number>
            %
            <div v-show="pm[3] > 100" class="red">* 输入的范围小于100%</div>
          </div>
          <div style="margin-bottom:20px;margin-left: 30px;">
            <el-input v-model="pmName[4]" style="width: 100px;" :disabled="true"></el-input>
            <el-input-number v-model="pm[4]" :min="0" class="ml10"></el-input-number>
            %
            <div v-show="pm[4] > 100" class="red">* 输入的范围小于100%</div>
          </div>
        </el-main>
      </el-container>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="pmQr">确定设置</el-button>
        <el-button @click="szDialogVisible = false">关闭</el-button>
        
      </span>
    </el-dialog>
    <el-dialog title="明细查询" :visible.sync="mxDialogVisible" :width="getDjmx()?'1800px':'1200px'" append-to-body>
      <div style="margin-bottom:20px">
        <el-table ref="zbABTable" :data="mxList" border stripe height="400px">
          <el-table-column label="序号" width="100" type="index" header-align="center" align="center">
          </el-table-column>
          <el-table-column prop="cpCode" label="票种" min-width="60" header-align="center" align="center">
            <template slot-scope="scope">
              {{ scope.row.cpCode }}票
            </template>
          </el-table-column>
          <el-table-column prop="cpCsl" label="总票数" min-width="80" header-align="center" align="center">
            <template slot-scope="scope">
              {{ scope.row.cpCsl }}票
            </template>
          </el-table-column>
          <el-table-column prop="cpDsl" label="提交票数" min-width="100" header-align="center" align="center">
            <template slot-scope="scope">
              {{ scope.row.cpDsl }}票
            </template>
          </el-table-column>
          <el-table-column prop="cpQz" label="票种权重" min-width="100" header-align="center" align="center">
            <template slot-scope="scope">
              {{ scope.row.cpQz }}%
            </template>
          </el-table-column>
          <el-table-column prop="cpQzpjf" label="平均分数" min-width="120" header-align="center" align="center">
          </el-table-column>
          <el-table-column prop="cpqz" label="测评项权重" min-width="120" header-align="center" align="center"
                           v-if="getDjmx()">
            <template slot-scope="scope">
              {{ scope.row.cpqz }}%
            </template>
          </el-table-column>
          <el-table-column prop="cpQzpjfCph" label="测评项分数" min-width="120" header-align="center" align="center"
                           v-if="getDjmx()">
          </el-table-column>
          <el-table-column prop="dcqz" label="等次项权重" min-width="120" header-align="center" align="center"
                           v-if="getDjmx()">
            <template slot-scope="scope">
              {{ scope.row.dcqz }}%
            </template>
          </el-table-column>
          <el-table-column prop="cpQzpjfDch" label="等次项分数" min-width="120" header-align="center" align="center"
                           v-if="getDjmx()">
          </el-table-column>
          <el-table-column prop="cpQzpjfZzfs" label="测评分数" min-width="100" header-align="center" align="center"
                           v-if="getDjmx()">
          </el-table-column>
          <el-table-column prop="cppQz" label="测评票权重" min-width="120" header-align="center" align="center"
                           v-if="getCppQz()">
            <template slot-scope="scope">
              {{ scope.row.cppQz }}%
            </template>
          </el-table-column>
          <el-table-column prop="cppFz" label="最终分数" min-width="100" header-align="center" align="center"
                           v-if="getCppQz()">
          </el-table-column>
        </el-table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="mxDialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="exportExcelAB">导出</el-button>
        
      </span>
    </el-dialog>

    <el-dialog title="详情查询" :visible.sync="xqDialogVisible" fullscreen append-to-body>
      <div style="margin-bottom:20px">
        <el-table ref="xqABTable" :data="tableData" border stripe height="calc(100vh - 207px)">
          <el-table-column label="序号" width="100" type="index" header-align="center" align="center">
          </el-table-column>
          <el-table-column prop="hdName" label="被测评" min-width="150" header-align="center" align="center"
                           :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column :prop="'hj_'+t1.id" :label="t1.zbMc + (t1.zbQz>0?('('+t1.zbQz+'%)'):'')" min-width="175"
                           header-align="center" align="center"
                           v-for="(t1,i1) in cepingXmmxbListTree" :key="i1*10000">
            <el-table-column :prop="'hj_'+t2.id" :label="t2.zbMc + (t2.zbQz>0?('('+t2.zbQz+'%)'):'')" min-width="175"
                             header-align="center"
                             align="center" v-if="t1.children" v-for="(t2,i2) in t1.children"
                             :key="i1*10000+i2*100">
              <el-table-column :prop="'hj_'+t3.id" :label="t3.zbMc + (t3.zbQz>0?('('+t3.zbQz+'%)'):'')" min-width="175"
                               header-align="center"
                               align="center" v-if="t2.children" v-for="(t3,i3) in t2.children"
                               :key="i1*10000+i2*100+i3">
              </el-table-column>
            </el-table-column>
          </el-table-column>
          <el-table-column prop="cpFz" label="指标汇总" min-width="150" header-align="center" align="center">
          </el-table-column>
          <!--          <el-table-column prop="cpDj" label="评价等次" min-width="150" header-align="center" align="center"-->
          <!--                           v-if="cepingHdglCppmxbInfo.djxxDjmx!=''">-->
          <!--          </el-table-column>-->
          <!--          <el-table-column prop="cpZf" label="评测成绩" min-width="150" header-align="center" align="center"-->
          <!--                           v-if="cepingHdglCppmxbInfo.djxxDjmx!=''">-->
          <!--          </el-table-column>-->
          <!--          <el-table-column prop="hdQz" label="测评权重" min-width="150" header-align="center" align="center">-->
          <!--            <template slot-scope="scope">-->
          <!--              {{ scope.row.hdQz }}%-->
          <!--            </template>-->
          <!--          </el-table-column>-->
          <!--          <el-table-column prop="qzZf" label="最终成绩" min-width="150" header-align="center" align="center">-->
          <!--          </el-table-column>-->
        </el-table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="exportExcelXq">导出</el-button>
        <el-button type="primary" plain @click="xqDialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {hzInfo, putObj} from '@/api/ceping/cepinghzglb'
import {hzInfo as hdhz} from '@/api/ceping/cepinghdglhdzhhzb'
import {bcpTjInfo} from '@/api/ceping/cepinghdglhdcphzb'
import FileSaver from 'file-saver'
import XLSX from 'xlsx'

export default {
  name: "cepinghztjDialog",
  data() {
    return {
      show: false,
      dialogVisible: false,
      szDialogVisible: false,
      mxDialogVisible: false,
      xqDialogVisible: false,
      cepingHzglb: {},
      cepingHzglDxpzbList: [],
      cepingHzglHdfwbList: [],
      jlts: 0,
      pm: [20, 20, 20, 20, 20],
      pmName: ["A", "B", "C", "D", "E"],
      pmShow: true,
      mxList: [],
      cepingHdglCppmxbInfo: {},
      cepingXmmxbListTree: [],
      tableData: [],//指标结构
    }
  },
  methods: {
    init(id) {
      const loading = this.$loading({
        lock: true,
        text: '正在汇总计算，请销后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      this.show = false;
      this.pmShow = true;
      hzInfo(id).then(res => {
        //console.log(res.data.data)
        this.cepingHzglb = res.data.data.cepingHzglb;
        if (this.cepingHzglb.hzJgbl) {
          this.pm = JSON.parse(this.cepingHzglb.hzJgbl).pm;
          this.pmName = JSON.parse(this.cepingHzglb.hzJgbl).pmName;
        } else {
          this.pm = [20, 20, 20, 20, 20];
          this.pmName = ["A", "B", "C", "D", "E"];
        }
        this.cepingHzglHdfwbList = res.data.data.cepingHzglHdfwbList;

        // for (var i in res.data.data.cepingHzglDxpzbList) {
        //   // res.data.data.cepingHzglDxpzbList[i].df = res.data.data.hzRestlt[i].df;
        //   for (var j in res.data.data.hzRestlt[i]) {
        //     if (j != "id") {
        //       res.data.data.cepingHzglDxpzbList[i][j] = res.data.data.hzRestlt[i][j];
        //     }
        //   }
        //
        // }

        function compareZb(a, b) {
          return b.df - a.df;
        }

        this.cepingHzglDxpzbList = res.data.data.hzRestlt.sort(compareZb);
        this.jlts = this.cepingHzglDxpzbList.length;
        loading.close();
        this.show = true;
        this.dialogVisible = true;
      })
    },
    pmQr() {
      let num = 0;
      for (var i in this.pm) {
        if (this.pm[i] == null) {
          this.pm[i] = 0;
          this.$set(this.pm, i, 0)
        }
        num += this.pm[i]
      }
      if (num.toFixed(2) != 100) {
        this.$notify.closeAll();
        this.$notify.error("您设置的排名比率总和为“" + num + "%”，不是100%，无法这样应用");
        return;
      }
      if (this.pm[1] > 0 && this.pm[0] == 0) {
        this.$notify.closeAll();
        this.$notify.error("您设置的排名" + this.pmName[1] + "为“" + this.pm[1] + "%”，但A为“0%”，无法这样应用");
        return;
      }
      if (this.pm[2] > 0 && this.pm[1] == 0) {
        this.$notify.closeAll();
        this.$notify.error("您设置的排名" + this.pmName[2] + "为“" + this.pm[2] + "%”，但B为“0%”，无法这样应用");
        return;
      }
      if (this.pm[3] > 0 && this.pm[2] == 0) {
        this.$notify.closeAll();
        this.$notify.error("您设置的排名" + this.pmName[3] + "为“" + this.pm[3] + "%”，但C为“0%”，无法这样应用");
        return;
      }
      if (this.pm[4] > 0 && this.pm[3] == 0) {
        this.$notify.closeAll();
        this.$notify.error("您设置的排名" + this.pmName[4] + "为“" + this.pm[4] + "%”，但D为“0%”，无法这样应用");
        return;
      }
      let data = {
        pm: this.pm,
        pmName: this.pmName
      }
      let info = {
        id: this.cepingHzglb.id,
        hzJgbl: JSON.stringify(data)
      }
      putObj(info).then(res => {
        this.szDialogVisible = false;
        this.init(this.cepingHzglb.id)
      })
    },
    /**
     * excel 导出
     */
    exportExcel(excelName) {
      try {
        const $e = this.$refs['cpphztj-table'].$el
        let $table = $e.querySelector('.el-table__fixed')
        if (!$table) {
          $table = $e
        }
        const wb = XLSX.utils.table_to_book($table, {
          raw: true
        })

        const wbout = XLSX.write(wb, {
          bookType: 'xlsx',
          bookSST: true,
          type: 'array'
        })
        FileSaver.saveAs(
          new Blob([wbout], {
            type: 'application/octet-stream'
          }),
          `${excelName}.xlsx`,
        )
      } catch (e) {
        if (typeof console !== 'undefined') console.error(e)
      }
    },
    showMx(row) {
      this.mxList = [];
      bcpTjInfo(this.cepingHzglb.id, this.cepingHzglb.hzDx == "部门" ? row.deptId : (row.bcpDx == "班子" ? row.deptId : row.userId), this.cepingHzglb.hzDx == "部门" ? 2 : (row.bcpDx == "班子" ? 2 : 1)).then(res => {
        this.mxList = this.shujuHandle(res.data.data);
        this.mxDialogVisible = true;
      });
    },
    shujuHandle(list) {
      //console.log(list)
      var data = [];
      var datajg = [];

      for (var a in list) {
        if (data.length > 0) {
          var bz = true;
          for (var b in data) {
            if (data[b].cpCode == list[a].cpCode) {
              data[b].cpCsl = data[b].cpCsl + list[a].cpCsl;
              data[b].cpDsl = data[b].cpDsl + list[a].cpDsl;
              data[b].cpPjf = data[b].cpPjf + list[a].cpPjf;
              data[b].cpQz = data[b].cpQz + list[a].cpQz;
              data[b].cpQzpjf = data[b].cpQzpjf + list[a].cpQzpjf;
              data[b].cpQzpjfCph = data[b].cpQzpjfCph + parseFloat(list[a].cpQzpjfCph);
              data[b].cpQzpjfDch = data[b].cpQzpjfDch + parseFloat(list[a].cpQzpjfDch);
              data[b].cpQzpjfZzfs = data[b].cpQzpjfZzfs + parseFloat(list[a].cpQzpjfZzfs);
              data[b].cpWsl = data[b].cpWsl + list[a].cpWsl;
              data[b].cppFz = data[b].cppFz + parseFloat(list[a].cppFz);
              data[b].cppQz = data[b].cppQz + list[a].cppQz;
              data[b].cpqz = data[b].cpqz + list[a].cpqz;
              data[b].dcqz = data[b].dcqz + list[a].dcqz;
              data[b].cs = data[b].cs + 1;

              bz = false;
            }
          }

          if (bz) {
            data.push({
              cpCode: list[a].cpCode,
              cpCsl: list[a].cpCsl,
              cpDsl: list[a].cpDsl,
              cpPjf: list[a].cpPjf,
              cpQz: list[a].cpQz,
              cpQzpjf: list[a].cpQzpjf,
              cpQzpjfCph: parseFloat(list[a].cpQzpjfCph),
              cpQzpjfDch: parseFloat(list[a].cpQzpjfDch),
              cpQzpjfZzfs: parseFloat(list[a].cpQzpjfZzfs),
              cpWsl: list[a].cpWsl,
              cppFz: parseFloat(list[a].cppFz),
              cppQz: list[a].cppQz,
              cpqz: list[a].cpqz,
              dcqz: list[a].dcqz,
              cs: 1,
            });
          }
        } else {
          data.push({
            cpCode: list[a].cpCode,
            cpCsl: list[a].cpCsl,
            cpDsl: list[a].cpDsl,
            cpPjf: list[a].cpPjf,
            cpQz: list[a].cpQz,
            cpQzpjf: list[a].cpQzpjf,
            cpQzpjfCph: parseFloat(list[a].cpQzpjfCph),
            cpQzpjfDch: parseFloat(list[a].cpQzpjfDch),
            cpQzpjfZzfs: parseFloat(list[a].cpQzpjfZzfs),
            cpWsl: list[a].cpWsl,
            cppFz: parseFloat(list[a].cppFz),
            cppQz: list[a].cppQz,
            cpqz: list[a].cpqz,
            dcqz: list[a].dcqz,
            cs: 1,
          });
        }
      }

      for (var c in data) {
        datajg.push({
          cpCode: data[c].cpCode,
          cpCsl: data[c].cpCsl,
          cpDsl: data[c].cpDsl,
          cpPjf: parseFloat((data[c].cpPjf / data[c].cs).toFixed(2)),
          cpQz: parseFloat((data[c].cpQz / data[c].cs).toFixed(2)),
          cpQzpjf: parseFloat((data[c].cpQzpjf / data[c].cs).toFixed(2)),
          cpQzpjfCph: parseFloat((data[c].cpQzpjfCph / data[c].cs).toFixed(2)),
          cpQzpjfDch: parseFloat((data[c].cpQzpjfDch / data[c].cs).toFixed(2)),
          cpQzpjfZzfs: parseFloat((data[c].cpQzpjfZzfs / data[c].cs).toFixed(2)),
          cpWsl: data[c].cpWsl,
          cppFz: parseFloat((data[c].cppFz / data[c].cs).toFixed(2)),
          cppQz: parseFloat((data[c].cppQz / data[c].cs).toFixed(2)),
          cpqz: parseFloat((data[c].cpqz / data[c].cs).toFixed(2)),
          dcqz: parseFloat((data[c].dcqz / data[c].cs).toFixed(2))
        });
      }

      //console.log(datajg)
      return datajg;
    },
    viewMx(row) {
      //console.log(row)
      let tableData = [];
      let hdids = [];
      for (var i in this.cepingHzglHdfwbList) {
        if (row["hd_" + this.cepingHzglHdfwbList[i].hdId]) {
          hdids.push(this.cepingHzglHdfwbList[i].hdId)
        }
      }
      let j = 0;
      for (let i in hdids) {
        setTimeout(() => {
          hdhz(hdids[i]).then(res => {
            j++;
            let cepingHdglb = res.data.data.cepingHdglb;
            let cepingHdglCppmxbList = res.data.data.cepingHdglCppmxbList;
            let cepingHdglHdcphzbList = res.data.data.cepingHdglHdcphzbList;
            //this.cepingXmmxbList = res.data.data.cepingXmmxbList;
            let cepingHdglHdzhhzbList = res.data.data.cepingHdglHdzhhzbList;
            let cepingHdglHdhzmxbList = res.data.data.cepingHdglHdhzmxbList;

            //填表
            let dataObj = {};
            let data = cepingHdglHdzhhzbList;
            for (var i in data) {
              dataObj[(data[i].bcpDx == '部门' ? "dept" : "user") + "_" + (data[i].bcpDx == '部门' ? data[i].bcpDeptId : data[i].bcpUserId)] = data[i];
            }

            for (var i in cepingHdglHdcphzbList) {
              var info = cepingHdglHdcphzbList[i];
              dataObj[(info.bcpDx == '部门' ? "dept" : "user") + "_" + (info.bcpDx == '部门' ? info.bcpDeptId : info.bcpUserId)]["cpFz"] = info.cpFz;
              dataObj[(info.bcpDx == '部门' ? "dept" : "user") + "_" + (info.bcpDx == '部门' ? info.bcpDeptId : info.bcpUserId)]["cpDj"] = info.cpDj;
              dataObj[(info.bcpDx == '部门' ? "dept" : "user") + "_" + (info.bcpDx == '部门' ? info.bcpDeptId : info.bcpUserId)]["cpZf"] = info.cpZf;
              dataObj[(info.bcpDx == '部门' ? "dept" : "user") + "_" + (info.bcpDx == '部门' ? info.bcpDeptId : info.bcpUserId)]["hdQz"] = info.hdQz;
              dataObj[(info.bcpDx == '部门' ? "dept" : "user") + "_" + (info.bcpDx == '部门' ? info.bcpDeptId : info.bcpUserId)]["qzZf"] = info.qzZf;
            }

            for (var i in cepingHdglHdhzmxbList) {
              var info = cepingHdglHdhzmxbList[i];
              dataObj[(info.bcpDx == '部门' ? "dept" : "user") + "_" + (info.bcpDx == '部门' ? info.bcpDeptId : info.bcpUserId)]["zb_" + info.zbId] = info.zbFz;
            }
            //console.log(dataObj)
            let dataInfo = dataObj[(this.cepingHzglb.hzDx == "部门" || (this.cepingHzglb.hzDx == "个人" && row.bcpDx == "班子") ? "dept_" : "user_") + (this.cepingHzglb.hzDx == "部门" || (this.cepingHzglb.hzDx == "个人" && row.bcpDx == "班子") ? row.deptId : row.userId)];
            dataInfo["hdName"] = cepingHdglb.hdName;

            let cppId = 0;
            for (var x in cepingHdglHdcphzbList) {
              let info = cepingHdglHdcphzbList[x];
              if (this.cepingHzglb.hzDx == "部门" || (this.cepingHzglb.hzDx == "个人" && row.bcpDx == "班子")) {
                if (info.bcpDeptId == row.deptId && info.bcpDx == "部门") {
                  if (j == 1) {
                    cppId = info.cppId;
                  }
                }
              } else {
                if (info.bcpUserId == row.userId && info.bcpDx == "个人") {
                  if (j == 1) {
                    cppId = info.cppId;
                  }
                }
              }
            }
            for (var x in cepingHdglCppmxbList) {
              let info = cepingHdglCppmxbList[x];
              //console.log(info)
              //dataInfo["hdQz"] = info.cppQz;
              if (info.cppId == cppId && j == 1) {
                let xmxxXmmx = info.xmxxXmmx;
                this.cepingHdglCppmxbInfo = info;
                this.cepingXmmxbListTree = this.getTree(JSON.parse(xmxxXmmx), 0);
              }
              //console.log(this.cepingXmmxbListTree)
            }

            //console.log(dataInfo)
            tableData.push(dataInfo);
            if (j >= hdids.length) {
              let d = JSON.parse(JSON.stringify(tableData[0]));
              d.hdName = (this.cepingHzglb.hzDx == "部门" || (this.cepingHzglb.hzDx == "个人" && row.bcpDx == "班子") ? row.deptName : row.userName);

              for (var i in tableData) {
                for (var k in tableData[i]) {
                  if (k.indexOf("zb_") != -1) {
                    if (d[k.replace("zb_", "hj_")] == undefined) {
                      if (this.cepingHzglb.hzFs == '公式') {
                        d[k.replace("zb_", "hj_")] = this.cepingHzglb.hzJsgs;
                        d[k.replace("zb_", "hj_")] = d[k.replace("zb_", "hj_")].replace("h" + tableData[i].hdId + "d", "" + (tableData[i][k] * 1 * tableData[i].hdQz / 100) + "");
                      } else {
                        d[k.replace("zb_", "hj_")] = tableData[i][k] * 1 * tableData[i].hdQz / 100;
                      }
                    } else {
                      if (this.cepingHzglb.hzFs == '公式') {
                        d[k.replace("zb_", "hj_")] = d[k.replace("zb_", "hj_")].replace("h" + tableData[i].hdId + "d", "" + (tableData[i][k] * 1 * tableData[i].hdQz / 100) + "");
                      } else {
                        d[k.replace("zb_", "hj_")] += tableData[i][k] * 1 * tableData[i].hdQz / 100;
                      }
                    }
                  }
                }
              }

              if (this.cepingHzglb.hzFs == '公式') {
                let reg = /h(.+?)d/;
                for (var k in d) {
                  if (k.indexOf("hj_") != -1) {
                    d[k] = d[k].replace(reg, "0.00");
                    try {
                      d[k] =  Math.round(eval(d[k]) * 100) / 100;
                    } catch (e) {
                      d[k] = '公式错误';
                    }
                  }
                }
              } else if (this.cepingHzglb.hzFs == '平均') {
                for (var k in d) {
                  if (k.indexOf("hj_") != -1) {
                    d[k] = Math.round(d[k] / tableData.length * 100) / 100;
                  }
                }
              } else {
                for (var k in d) {
                  if (k.indexOf("hj_") != -1) {
                    d[k] = Math.round(d[k] * 100) / 100;
                  }
                }
              }

              d.cpFz = row.df;
              this.tableData = [d];
              this.xqDialogVisible = true;
            }
          })
        }, i * 100)
      }
    },
    getDjmx() {
      let r = false;
      for (var i in this.mxList) {
        if (this.mxList[i].cpqz != 100) {
          return true;
        }
      }
      return r;
    },
    getCppQz() {
      let r = false;
      for (var i in this.mxList) {
        if (this.mxList[i].cppQz != 100) {
          return true;
        }
      }
      return r;
    },
    getTree(ary, pid) {
      return ary.filter(v => {
        if (v.pid === pid) {
          if (v.pid === 0) {
            v.xh = 1;
          } else {
            for (var a = 0; a < ary.length; a++) {
              if (ary[a].id == v.pid) {
                v.xh = ary[a].xh + 1;
              }
            }
          }
          return v
        }
      }).map(v => {
        const children = this.getTree(ary, v.id);
        if (children.length) v.children = this.getTree(ary, v.id);
        return v
      });
    },
    exportExcelAB() {
      const excelName = this.cepingHzglb.hzName + "_明细"
      try {
        const $e = this.$refs['zbABTable'].$el
        let $table = $e.querySelector('.el-table__fixed')
        if (!$table) {
          $table = $e
        }
        const wb = XLSX.utils.table_to_book($table, {
          raw: true
        })

        const wbout = XLSX.write(wb, {
          bookType: 'xlsx',
          bookSST: true,
          type: 'array'
        })
        FileSaver.saveAs(
          new Blob([wbout], {
            type: 'application/octet-stream'
          }),
          `${excelName}.xlsx`,
        )
      } catch (e) {
        if (typeof console !== 'undefined') console.error(e)
      }
    },
    exportExcelXq() {
      const excelName = this.cepingHzglb.hzName + "_详情"
      try {
        const $e = this.$refs['xqABTable'].$el
        let $table = $e.querySelector('.el-table__fixed')
        if (!$table) {
          $table = $e
        }
        const wb = XLSX.utils.table_to_book($table, {
          raw: true
        })

        const wbout = XLSX.write(wb, {
          bookType: 'xlsx',
          bookSST: true,
          type: 'array'
        })
        FileSaver.saveAs(
          new Blob([wbout], {
            type: 'application/octet-stream'
          }),
          `${excelName}.xlsx`,
        )
      } catch (e) {
        if (typeof console !== 'undefined') console.error(e)
      }
    }
  }
}
</script>

<style scoped>
</style>
